<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
</head>
<body>
    <div class="layui-form" style="padding-right: 30px">
        <div class="layui-form-item">
            <label class="layui-form-label">用户ID</label>
            <div class="layui-input-block">
                <input type="text" name="uId" lay-verify="required" autocomplete="off" placeholder="请输入用户ID" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-block">
                <input type="text" name="pName" lay-verify="required" autocomplete="off" placeholder="请输入商品名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品图片</label>
            <div class="layui-input-block">
                <input type="text" name="pPicture" lay-verify="required" lay-reqtext="用户名是必填项，岂能为空？" placeholder="请输入商品图片" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品描述</label>
            <div class="layui-input-block">
                <input type="text" name="pDesc" lay-verify="required" autocomplete="off" placeholder="请输入用户ID" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品预警数量</label>
            <div class="layui-input-block">
                <input type="text" name="pWarnCount" lay-verify="required" autocomplete="off" placeholder="请输入用户ID" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品总数量</label>
            <div class="layui-input-block">
                <input type="text" name="pAllCount" lay-verify="required" autocomplete="off" placeholder="请输入用户ID" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-block">
                <input type="text" name="pPrice" lay-verify="required" autocomplete="off" placeholder="请输入用户ID" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品一级分类</label>
            <div class="layui-input-block">
                <input type="text" name="pOneClassify" lay-verify="required" autocomplete="off" placeholder="请输入用户ID" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品二级分类</label>
            <div class="layui-input-block">
                <input type="text" name="pTwoClassify" lay-verify="required" autocomplete="off" placeholder="请输入用户ID" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addProduct">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </div>

    <script th:src="@{/layui/layui.js}"></script>

    <script>
        layui.use(['form'],function(){
            var form = layui.form;
            var $ = layui.$;

            // // 自定义验证
            // form.verify({
            //     username: function(value, item) { //value：表单的值、item：表单的DOM对象
            //         if(value.length < 5 || value.length > 10) {
            //             return "用户名必须在5到10位之间";
            //         }
            //     }
            // });
            /*  监听表单提交(添加商品)*/
            form.on("submit(addProduct)",function(data) {
                console.log(data);

                $.ajax({
                    type:"post",
                    data:data.field,
                    url:"[[@{/product/addProductPage}]]",
                    success:function(result) {
                       console.log(result);
                        if(result.code == 200) {
                            layer.msg("添加商品成功",{
                                time:1000
                            },function() {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭弹窗
                                parent.layer.close(index);
                                //窗口刷新
                                parent.location.reload();
                            })

                        } else {
                            layer.msg(result.msg,{
                                time:1000
                            })
                        }
                    }
                });
            });

        });
    </script>
</body>
</html>